package com.zhongke.cotrun.controller;

import com.github.pagehelper.util.StringUtil;
import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.pojo.NeedBidding;
import com.zhongke.cotrun.model.pojo.NeedInfo;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.NeedBiddingService;
import com.zhongke.cotrun.service.NeedInfoService;

import io.swagger.annotations.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
/**
 * @Description: 用户需求竞标留言
 * @Author: songjing
 * @Date:   2020-03-19
 * @Version: V1.0
 */
@Api(tags = "用户需求竞标留言相关接口")
@RestController
@RequestMapping("/needInfoBidding")
@Validated
public class NeedBiddingController {
    
    @Autowired
    NeedBiddingService needBiddingService;
    @Autowired
    NeedInfoService needInfoService;
    
    private final Logger logger = LoggerFactory.getLogger(NeedBiddingController.class);
   
    @ApiOperation(value = "查看指定需求所有竞标留言")
    @GetMapping(value= {"/selectAllByNeedInfo"},produces = "application/json;charset=utf-8")
    public APIResponse selectAllByMine(@NotNull(message = "需求id不能为空") Long infoId,
    		@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10")Integer pageSize) {    	
    	return APIResponse.returnSuccess(needBiddingService.selectListByInfoId(infoId, pageNum, pageSize));

    }
    
    @ApiOperation(value = "新增竞标留言")
    @PostMapping("/addNeedBidding")
    public APIResponse addNeedBidding(NeedBidding needBidding){
    	if(needBidding.getInfoId() == null || needBidding.getInfoId() == 0
    			|| needBidding.getUserInfoId() == null || needBidding.getUserInfoId() == 0 
    			|| StringUtil.isEmpty(needBidding.getNote()))
    		 return APIResponse.returnFail(CodeMsg.BIND_ERROR);
    	NeedInfo needInfo = needInfoService.selectDetailById(needBidding.getInfoId());
    	if(needInfo == null)
    		 return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
    	needBidding.setCreateBy(new Long(needBidding.getUserInfoId()));    	
        if(needBiddingService.add(needBidding)){
            return APIResponse.returnSuccess("添加成功");
        }else{
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }
   
    @ApiOperation(value = "删除竞标留言")
    @GetMapping(value= {"/deleteNeedBidding"},produces = "application/json;charset=utf-8")
    public APIResponse deleteNeedInfo(@NotNull(message = "竞标主键不能为空") Long id){
    	if(needBiddingService.delete(id)){
	      return APIResponse.returnSuccess("删除成功");
	    }else{
	      return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
	    }
    }
 
   
    @ApiOperation(value = "竞标留言状态更新")
    @GetMapping(value= {"/updateStateNeedBidding"},produces = "application/json;charset=utf-8")
    public APIResponse updateStateNeedBidding(@NotNull(message = "竞标主键不能为空") Long id){
    	if(needBiddingService.updateState(id)){
	      return APIResponse.returnSuccess("更新状态成功");
	    }else{
	      return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
	    }
    }
}
